#ifndef REUTERSDATAMODEL_HPP
#define REUTERSDATAMODEL_HPP

#include <QObject>
#include <QHash>
#include <QList>
#include <QSet>
#include <QString>
#include <QtAlgorithms>

class ReutersDocument;

class ReutersDataModel : public QObject
{
    Q_OBJECT
public:
    explicit ReutersDataModel(QObject *parent = 0);
    
    ReutersDocument* document (const QString& docFile);

    const QSet<ReutersDocument*>& docsInClass(const QString& cls);

    ReutersDocument* createDocument (const QString& file, const QString& title);

    QList<ReutersDocument*>& allDocuments () {
        return documents_;
    }

    void assignClass (const QString& id, const QString& cls);

    QList<QString> getClasses() {
        QList<QString> classes = docsByClass_.keys();
        qSort(classes);
        return classes;
    }

signals:
    
public slots:
    
private:

    QSet<ReutersDocument*> BLANK_DOCSET;

    QList<ReutersDocument*> documents_;

    QHash<QString, QSet<ReutersDocument*> > docsByClass_;

    QHash<QString, ReutersDocument*> docsByFile_;

};


/*===========================================================================*/


inline ReutersDocument* ReutersDataModel::document (const QString& docFile) {
    return docsByFile_.value(docFile, nullptr);
}


inline const QSet<ReutersDocument*>& ReutersDataModel::docsInClass(const QString& cls) {
    QHash<QString, QSet<ReutersDocument*> >::iterator docset = docsByClass_.find(cls);
    return docsByClass_.end() == docset ? BLANK_DOCSET : *docset;
}


#endif // REUTERSDATAMODEL_HPP
